package org.vectrics.recruiter.xmlweb;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.vectrics.SystemException;
import org.vectrics.dataweb.WebServiceAction;
import org.vectrics.mail.struts.AjaxUtils;
import org.vectrics.mail.struts.LetterStagingManager;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.Request;
import org.vectrics.user.Person;
import org.w3c.dom.Element;

public class SaveStagedLetterWebService extends WebServiceAction {
	public static String COMMAND = "save.letter";

	public SaveStagedLetterWebService() {
		super();
		// TODO Auto-generated constructor stub
	}

	
	public void getData(Request request, String command,
			Element requestElement, Element responseElement) {
		log.info("getData() called");
		String subject = request.getParameter("subject");
		String body = request.getParameter("body");
		String senderIdParam = request.getParameter("senderId");
		Long senderId = new Long(senderIdParam);
		Person sender = CoreServiceLocator.getPersonService().findPersonById(senderId);
		if (sender == null) {
			throw new SystemException("Could not find sender: " + senderId);
		}

		String recipientIdParam = request.getParameter("recipientId");
		Long recipientId = new Long(recipientIdParam);
		Person recipient = CoreServiceLocator.getPersonService().findPersonById(recipientId);
		if (recipient == null) {
			throw new SystemException("Could not find sender: " + recipientId);
		}
		log.info("getData() - sender = " + senderId + "  recipientId=" + recipientId); 
		
		String dateMillisParam = request.getParameter("time");
		Long time = new Long(dateMillisParam);
		Date stageDate = new Date(time.longValue());
		
		LetterStagingManager stagingManager = new LetterStagingManager(sender, stageDate);
		String oldBody = stagingManager.getLetterContents(recipient);
		if (body.equals(oldBody) == false) {
			log.info("Saving recient letter body" +
					"\r\n - New body: " + body +
					"\r\n - Old body: " + oldBody
					);
			stagingManager.saveBody(recipient, body);
		} else {
			log.info("Submtted letter body hasn't changed, not saving");
		}
		
		String oldSubject = stagingManager.getSubject(recipient);
		if (subject.equals(oldSubject) == false) {
			log.info("Saving recient letter subject" +
					"\r\n - New subject: " + subject +
					"\r\n - Old subject: " + oldSubject
					);
			stagingManager.saveSubject(recipient, body);
		} else {
			log.info("Submitted subject hasn't change, not saving");
		}
		
	}
/*	
	public void getData(HttpServletRequest request, String command,
			Element requestElement, Element responseElement) {
		log.info("getData() called");
		if (!command.equals(COMMAND)) {
			throw new SystemException("Do not know how to handle web service command: " + command);
		} else {
			String senderIdParam = request.getParameter("senderId");
			Long senderId = new Long(senderIdParam);
			Person sender = CoreServiceLocator.getPersonService().findPersonById(senderId);
			if (sender == null) {
				throw new SystemException("Could not find sender: " + senderId);
			}

			String recipientIdParam = request.getParameter("recipientId");
			Long recipientId = new Long(recipientIdParam);
			Person recipient = CoreServiceLocator.getPersonService().findPersonById(recipientId);
			if (recipient == null) {
				throw new SystemException("Could not find sender: " + recipientId);
			}
			log.info("getData() - sender = " + senderId + "  recipientId=" + recipientId); 
			
			String dateMillisParam = request.getParameter("time");
			Long time = new Long(dateMillisParam);
			Date stageDate = new Date(time.longValue());
			LetterStagingManager stagingManager = new LetterStagingManager(sender, stageDate);
			String letterContents = stagingManager.getLetterContents(recipient);
			log.info("Letter contents: " + letterContents);
			
			Element letterElement = responseElement.getOwnerDocument().createElement("letter");
			this.letterToElement(sender, recipient, letterContents , letterElement);
			Element dataElement = this.getDataElement(responseElement);
			dataElement.appendChild(letterElement);
		}
	}

	
	private void letterToElement(Person sender, Person recipient, String letterContent, Element letterElement) {
		letterElement.setAttribute("senderId", sender.getId().toString());
		letterElement.setAttribute("recipientId", recipient.getId().toString());
		letterElement.setAttribute("recipientName", recipient.getFullName());

		AjaxUtils ajaxUtils = new AjaxUtils();
		String encodedLetterContent = ajaxUtils.encodeLineBreaks(letterContent); 
		letterElement.setAttribute("body", encodedLetterContent);
	}
	*/
	
}
